<!--
 * @Description:
 * @Author bianpengfei
 * @create 2021/3/30 17:31
 -->
<template>
  <div></div>
</template>

<script>
import { sync, call } from 'vuex-pathify'
import Loading from '@/libs/layouts/backsys/ant-loading'
export default {
  data() {
    return {
      code: ''
    }
  },

  computed: {
    loading: sync('app/loading')
  },

  components: {
    Loading
  },

  async created() {
    this.loading = true
    const { code } = this.$route.query
    if (code) {
      this.code = code
      await this.handleLogin()
    } else {
      this.ledLogOut().then(() => {
        window.location.reload()
      })
    }
  },

  mounted() {},

  methods: {
    ...call('user', ['autoLogin', 'ledLogOut', 'getInfo']),
    _setDynamicRoutes: call('app/setDynamicRoutes'),

    handleLogin() {
      return new Promise(async (resolve, reject) => {
        try {
          localStorage.setItem('login_time', JSON.stringify(Number(new Date())))
          await this.autoLogin(this.code)
          const data = await this.getInfo()
          const routes = await this._setDynamicRoutes(data.appPermissions)
          this.$router.addRoutes(routes)
          await initEnum()
          this.$router.push({ path: '/' })
        } catch (e) {
          console.error(e)
          this.$message.error('错了哦，登录失败！账号名或者密码错误')
        }
        resolve()
      })
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .ant-loading {
  width: 84px;
  height: 84px;
  position: absolute;
  top: 110px;
  left: 0;
  right: 0;
  margin: auto;
  z-index: 100;
}

.loading-text {
  width: 84px;
  height: 84px;
  position: absolute;
  top: 200px;
  left: 0;
  right: 0;
  margin: auto;
  z-index: 100;
}
</style>
